## How do people evaluate foreign aid to “nasty” regimes?
## Tobias Heinrich & Yoshiharu Kobayashi
## British Journal of Political Science
#########################################################

## Prepping the MTurk and CCES data


## 1) Prep the MTurk data
#########################
## Load the results from MTurk drop all survey responses which were taken by the authors
dt <- read.csv2("Data Export 2014_08_21.csv", stringsAsFactors=FALSE)
dt <- subset(dt, INPUTZIP != 99999)
dt$X <- NULL

## Omit cases with people doing odd things (failing screeners too often, 
## taking way to long, rushing through). Loss of 124 surveys
dt <- subset(dt, SCREENER_FAILURES_Q3 < 4)
dt <- subset(dt, as.numeric(Q5_TIME) < 800)
dt <- subset(dt, as.numeric(Q5_TIME) > 40)


## Restructure the data frame
data <- adply(.data=1:nrow(dt), .margins=1, .fun=function(i) format_raw_data(x=dt[i,]),
              .progress="text")
## Top-code income ($150k and more are 12)
data$Income[data$Income > 11] <- 12 
## Combine "Less than High school" and "High school" into one
data$Education[data$Education == 1] <- 2
data$Age <- 2014 - data$BirthYr
data$TC <- data$C + data$RA
data$RT[data$PI %in% c(2, 3, 4, 5)] <- data$RT[data$PI %in% c(2, 3, 4, 5)] + 1
data$RT[data$RA == 0] <- 0
colnames(data)[1] <- "ID"
save(data, file="output/Modified MTurk Data.Rdata")


## 2) Prep CCES data
####################
if(redo_CCES == TRUE)
{
  cces <- read.dta("cces_common_cumulative_4.dta")
  cces <- subset(cces, year == 2012)
  cces <- cces[, c("weight", "birthyr", "gender", "educ", "employ",
                   "pid7", "economy_retrospective")]
  ## Recoding variables so that they match the numerical coding of our MTurk data
  ## Gender
  cces$Gender <- ifelse(cces$gender == "Female", 2, 1)
  cces$gender <- NULL
  ## Occupation
  cces$Occupation <- 1
  cces$Occupation[cces$employ == "Part-time"] <- 2
  cces$Occupation[cces$employ == "Temporarily laid off"] <- 3
  cces$Occupation[cces$employ == "Unemployed"] <- 4
  cces$Occupation[cces$employ == "Retired"] <- 5
  cces$Occupation[cces$employ == "Permanently disabled"] <- 6
  cces$Occupation[cces$employ == "Homemaker"] <- 7
  cces$Occupation[cces$employ == "Student"] <- 8
  cces$employ <- NULL
  ##Education
  cces$Education <- 1
  cces$Education[cces$educ == "High school graduate"] <- 2
  cces$Education[cces$educ == "Some college"] <- 3
  cces$Education[cces$educ == "2-year"] <- 4
  cces$Education[cces$educ == "4-year"] <- 5
  cces$Education[cces$educ == "Post-grad"] <- 6
  cces$educ <- NULL
  cces$Education[cces$Education == 1] <- 2
  ## RepDem 
  cces$RepDem <- 1 
  cces$RepDem[cces$pid7 == "Not very strong Democrat"] <- 2
  cces$RepDem[cces$pid7 == "Lean Democrat"] <- 3
  cces$RepDem[cces$pid7 == "Independent"] <- 4
  cces$RepDem[cces$pid7 == "Lean Republican"] <- 5
  cces$RepDem[cces$pid7 == "Not very strong Republican"] <- 6
  cces$RepDem[cces$pid7 == "Strong Republican"] <- 7
  cces$pid7 <- NULL
  ## Birthyr
  colnames(cces)[which(colnames(cces) == "birthyr")] <- "BirthYr"
  ## EconChange
  cces$EconChange <- 0
  cces$EconChange[cces$economy_retrospective == "Gotten much better"] <- 1
  cces$EconChange[cces$economy_retrospective == "Gotten better"] <- 2
  cces$EconChange[cces$economy_retrospective == "Stayed about the same"] <- 3
  cces$EconChange[cces$economy_retrospective == "Gotten worse"] <- 4
  cces$EconChange[cces$economy_retrospective == "Gotten much worse"] <- 5
  cces <- subset(cces, EconChange > 0)
  cces$economy_retrospective <- NULL
  
  ## Resample in proportion to survey weights 
  cces <- cces[sample(1:nrow(cces), replace=TRUE, size=nrow(cces),
                      prob=cces$weight),]
  cces$Age <- 2014 - cces$BirthYr
  save(cces, file="output/Modified CCES Data.Rdata")
}
if(redo_CCES == FALSE) file.copy(from="Modified CCES Data.Rdata",
                                 to="output/Modified CCES Data.Rdata",
                                 overwrite = TRUE)




